package me.khrystal.rxnews.module.news.ui;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import me.khrystal.rxnews.R;
import me.khrystal.rxnews.annotation.ActivityFragmentInject;
import me.khrystal.rxnews.base.BaseActivity;
import me.khrystal.rxnews.base.BaseSpacesItemDecoration;
import me.khrystal.rxnews.callback.OnItemClickAdapter;
import me.khrystal.rxnews.callback.SimpleItemTouchHelperCallback;
import me.khrystal.rxnews.greendao.NewsChannelTable;
import me.khrystal.rxnews.module.news.presenter.INewsChannelPresenter;
import me.khrystal.rxnews.module.news.presenter.INewsChannelPresenterImpl;
import me.khrystal.rxnews.module.news.ui.adapter.NewsChannelAdapter;
import me.khrystal.rxnews.module.news.view.INewsChannelView;
import me.khrystal.rxnews.utils.ClickUtils;
import me.khrystal.rxnews.utils.MeasureUtil;
import me.khrystal.rxnews.utils.TextViewUtil;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/29
 * update time:
 * email: 723526676@qq.com
 */

@ActivityFragmentInject(
        contentViewId = R.layout.activity_news_channel,
        menuId = R.menu.menu_settings,
        enableSlidr = true,
        toolbarTitle = R.string.news_channel_manage
)
public class NewsChannelActivity extends BaseActivity<INewsChannelPresenter>
        implements INewsChannelView{

    @Override
    protected void initView() {
        TextView myTextView = (TextView) findViewById(R.id.tv_my_channel);
        final float textSize = myTextView.getTextSize();
        int startPos = myTextView.getText().toString().lastIndexOf(" ");
        TextViewUtil.setPartialSize(myTextView,startPos,myTextView.getText().toString().length()
            ,(int)(textSize * 3 * 1.0f / 4));
        mPresenter = new INewsChannelPresenterImpl(this);
    }

    /**
     * 初始化两个recyclerview
     * @param selectChannels
     * @param unselectChannels
     */
    @Override
    public void initTwoRecyclerView(List<NewsChannelTable> selectChannels, List<NewsChannelTable> unselectChannels) {

        //初始化我的频道RecyclerView
        final RecyclerView recyclerView1 = (RecyclerView)findViewById(R.id.recycler_view);
        recyclerView1.setLayoutManager(
                new GridLayoutManager(this,4, LinearLayoutManager.VERTICAL,false));
        recyclerView1.addItemDecoration(new BaseSpacesItemDecoration(MeasureUtil.dip2px(this,8)));
        recyclerView1.setItemAnimator(new DefaultItemAnimator());
        recyclerView1.getItemAnimator().setAddDuration(250);
        recyclerView1.getItemAnimator().setMoveDuration(250);
        recyclerView1.getItemAnimator().setChangeDuration(250);
        recyclerView1.getItemAnimator().setRemoveDuration(250);

        final NewsChannelAdapter mRecyclerAdapter1 = new NewsChannelAdapter(this,selectChannels,false);
        recyclerView1.setAdapter(mRecyclerAdapter1);

        SimpleItemTouchHelperCallback callback1 = new SimpleItemTouchHelperCallback(mRecyclerAdapter1);
        ItemTouchHelper itemTouchHelper1 = new ItemTouchHelper(callback1);

        mRecyclerAdapter1.setItemTouchHelper(callback1);
        mRecyclerAdapter1.setItemMoveListener(new NewsChannelAdapter.OnItemMoveListener() {
            @Override
            public void onItemMove(int fromPosition, int toPosition) {
                mPresenter.onItemSwap(fromPosition,toPosition);
            }
        });

        final RecyclerView recyclerView2 = (RecyclerView)findViewById(R.id.recycler_view2);
        recyclerView2.setLayoutManager(
                new GridLayoutManager(this,4,LinearLayoutManager.VERTICAL,false)
        );
        recyclerView2.addItemDecoration(new BaseSpacesItemDecoration(MeasureUtil.dip2px(this, 8)));
        recyclerView2.setItemAnimator(new DefaultItemAnimator());
        recyclerView2.getItemAnimator().setAddDuration(250);
        recyclerView2.getItemAnimator().setMoveDuration(250);
        recyclerView2.getItemAnimator().setChangeDuration(250);
        recyclerView2.getItemAnimator().setRemoveDuration(250);

        final NewsChannelAdapter mRecyclerAdapter2 = new NewsChannelAdapter(this,unselectChannels,false);
        recyclerView2.setAdapter(mRecyclerAdapter2);

        mRecyclerAdapter1.setOnItemClickListener(new OnItemClickAdapter() {
            @Override
            public void onItemClick(View view, int position) {
                if (ClickUtils.isFastDoubleClick()){
                    return;
                }
                if (!mRecyclerAdapter1.getData().get(position).getNewsChannelFixed()){
                    mPresenter.onItemAddOrRemove(
                            mRecyclerAdapter1.getData().get(position).getNewsChannelName(),false);
                    mRecyclerAdapter2.add(mRecyclerAdapter2.getItemCount(),mRecyclerAdapter1.getData().get(position));
                    mRecyclerAdapter1.delete(position);
                }
            }
        });
        mRecyclerAdapter2.setOnItemClickListener(new OnItemClickAdapter() {
            @Override
            public void onItemClick(View view, int position) {
                if (ClickUtils.isFastDoubleClick()){
                    return;
                }
                mPresenter.onItemAddOrRemove(
                        mRecyclerAdapter2.getData().get(position).getNewsChannelName(),true);
                mRecyclerAdapter1.add(mRecyclerAdapter1.getItemCount(),mRecyclerAdapter2.getData().get(position));
                mRecyclerAdapter2.delete(position);
            }
        });
    }
}
